﻿/*****************************************************************************
 
 Use of Ext is under the Ext JS license, for more information, 
 please visit http://extjs.com/products/license.php page. 
  
 You should confirm that you have read, understood and accept ethical criteria listed. 
 If not, see <http://www.mvcnext.com/license/>.
  
 Version: 1.0.0.0
 Website: http://www.mvcnext.com/
 
 Copyright (c) Yoganand R. All rights reserved.
 For continued development and updates, visit http://yogsbreath.net
 
 ******************************************************************************/
using System;
using System.Collections.Generic;
using System.Web.UI.WebControls;
using System.ComponentModel;
using System.IO;
using System.Reflection;
using System.Collections;
using System.Web.UI;

namespace MVCnExt.Web.UI.Ext
{
    /// <summary>
    /// Ext.form.FormPanel are classes derived from Panel and the library provides two ways to build a form: BasicForm and FormPanel.
    /// </summary>
    public class ExtTextField : Field  
    {
        public override List<ScriptReference> GetScriptReferences()
        {
            return base.GetScriptReferences();
        }

        public override IList<ScriptDescriptor> GetScriptDescriptors()
        {
            IList<ScriptDescriptor> list = base.GetScriptDescriptors();

            ScriptControlDescriptorBase desc = (ScriptControlDescriptorBase)list[0];

            desc.Type = "Ext.form.TextField";

            String[] properties = new String[]{
                "allowBlank", "blankText", "disableKeyFilter", "emptyClass", "emptyText", "grow",  "growMax", "growMin",
                "maskRe", "maxLength", "maxLengthText", "regex", "regexText", "selectOnFocus", "truncate", "validator", 
                "vtype", "vtypeText"
            };

            Type component = typeof(ExtTextField);

            foreach (String property in properties)
            {
                PropertyInfo i = component.GetProperty(property);

                if (i == null)
                    throw new InvalidOperationException(String.Format("Property not found, 'ExtTextField.{0}'", property));

                Object value = i.GetValue(this, null);

                //get the default value attribute
                if (value == null)
                {
                    // Gets the attributes for the property.
                    System.ComponentModel.AttributeCollection attributes =
                       TypeDescriptor.GetProperties(this)[property].Attributes;

                    /* get the default value by retrieving the DefaultValueAttribute 
                       from the AttributeCollection. */
                    DefaultValueAttribute defaultAttribute =
                       (DefaultValueAttribute)attributes[typeof(DefaultValueAttribute)];

                    if (defaultAttribute != null)
                        value = defaultAttribute.Value;
                }

                if (value == null)
                {
                    continue;
                }

                if (!(value is String))
                    desc.AddPropertyBase(property, value);
                else
                    desc.AddPropertyBase(property, String.Format("'{0}'", value));
            }                        

            return list;
        }

        #region Public Properties

        /// <summary>
        /// False to validate that the value length > 0 (defaults to true).
        /// </summary>
        [DefaultValue(true)]
        [Description("False to validate that the value length > 0 (defaults to true).")]
        public Boolean? allowBlank { get; set; }

        /// <summary>
        /// Error text to display if the allow blank validation fails (defaults to 'This field is required').
        /// </summary>
        [Description("Error text to display if the allow blank validation fails (defaults to 'This field is required').")]
        public String blankText { get; set; }

        /// <summary>
        /// True to disable input keystroke filtering (defaults to false).
        /// </summary>
        [Description("True to disable input keystroke filtering (defaults to false).")]
        public Boolean? disableKeyFilter { get; set; }

        /// <summary>
        /// The CSS class to apply to an empty field to style the emptyText (defaults to 'x-form-empty-field'). This class is automatically added and removed as needed depending on the current field value.
        /// </summary>
        [Description("The CSS class to apply to an empty field to style the emptyText (defaults to 'x-form-empty-field'). This class is automatically added and removed as needed depending on the current field value.")]
        public String emptyClass { get; set; }

        /// <summary>
        /// The default text to display in an empty field (defaults to null).
        /// </summary>
        [Description("The default text to display in an empty field (defaults to null).")]
        public String emptyText { get; set; }

        /// <summary>
        /// True to enable the proxying of key events for the HTML input field (defaults to false)
        /// </summary>
        [Description("True to enable the proxying of key events for the HTML input field (defaults to false)")]
        public Boolean? enableKeyEvents { get; set; }

        /// <summary>
        /// True if this field should automatically grow and shrink to its content (defaults to false).
        /// </summary>
        [Description("True if this field should automatically grow and shrink to its content (defaults to false).")]
        public String grow { get; set; }

        /// <summary>
        /// The maximum width to allow when grow = true (defaults to 800).
        /// </summary>
        [DefaultValue(typeof(Unit), "800")]
        [Description("The maximum width to allow when grow = true (defaults to 800).")]
        public Unit? growMax { get; set; }

        /// <summary>
        /// The minimum width to allow when grow = true (defaults to 30).
        /// </summary>
        [DefaultValue(typeof(Unit), "30")]
        [Description("The minimum width to allow when grow = true (defaults to 30).")]
        public Unit? growMin { get; set; }

        /// <summary>
        /// An input mask regular expression that will be used to filter keystrokes that don't match (defaults to null).
        /// </summary>
        [Description("An input mask regular expression that will be used to filter keystrokes that don't match (defaults to null).")]
        public String maskRe { get; set; }

        /// <summary>
        /// The type attribute for input fields.
        /// </summary>        
        [DefaultValue(InputType.Text)]
        [Description("The type attribute for input fields.")]
        public InputType inputType { get; set; }

        /// <summary>
        /// Maximum input field length allowed (defaults to Number.MAX_VALUE).
        /// </summary>
        [Description("Maximum input field length allowed (defaults to Number.MAX_VALUE).")]
        public int? maxLength { get; set; }

        /// <summary>
        /// Error text to display if the maximum length validation fails (defaults to 'The maximum length for this field is {maxLength}').
        /// </summary>
        [Description("Error text to display if the maximum length validation fails (defaults to 'The maximum length for this field is {maxLength}').")]
        public String maxLengthText { get; set; }

        /// <summary>
        /// A JavaScript RegExp object to be tested against the field value during validation (defaults to null). If available, this regex will be evaluated only after the basic validators all return true, and will be passed the current field value. If the test fails, the field will be marked invalid using RegexText.
        /// </summary>
        [Description("A JavaScript RegExp object to be tested against the field value during validation (defaults to null). If available, this regex will be evaluated only after the basic validators all return true, and will be passed the current field value. If the test fails, the field will be marked invalid using RegexText.")]
        public String regex { get; set; }

        /// <summary>
        /// The error text to display if regex is used and the test fails during validation (defaults to '').
        /// </summary>
        [Description("The error text to display if regex is used and the test fails during validation (defaults to '').")]
        public String regexText { get; set; }

        /// <summary>
        /// True to automatically select any existing field text when the field receives input focus (defaults to false).
        /// </summary>
        [Description("True to automatically select any existing field text when the field receives input focus (defaults to false).")]
        public Boolean? selectOnFocus { get; set; }

        /// <summary>
        /// If MaxLength property has been set, more characters than the MaxLength can be entered if Truncate='false'. If 'false', then a validation error is rendered if more characters entered (or pasted) than the MaxLength property. Default value is 'true'.
        /// </summary>
        [DefaultValue(true)]
        [Description("If MaxLength property has been set, more characters than the MaxLength can be entered if Truncate='false'. If 'false', then a validation error is rendered if more characters entered (or pasted) than the MaxLength property. Default value is 'true'.")]
        public Boolean? truncate { get; set; }

        /// <summary>
        /// A custom validation function to be called during field validation (defaults to null). If available, this function will be called only after the basic validators all return true, and will be passed the current field value and expected to return boolean true if the value is valid or a string error message if invalid.
        /// </summary>
        [Description("A custom validation function to be called during field validation (defaults to null). If available, this function will be called only after the basic validators all return true, and will be passed the current field value and expected to return boolean true if the value is valid or a string error message if invalid.")]
        public String validator { get; set; }

        /// <summary>
        /// A validation type name as defined in Ext.form.VTypes (defaults to null).
        /// </summary>
        [Description("A validation type name as defined in Ext.form.VTypes (defaults to null).")]
        public String vtype { get; set; }

        /// <summary>
        /// A custom error message to display in place of the default message provided for the vtype currently set for this field (defaults to ''). Only applies if vtype is set, else ignored.
        /// </summary>
        [Description("A custom error message to display in place of the default message provided for the vtype currently set for this field (defaults to ''). Only applies if vtype is set, else ignored.")]
        public String vtypeText { get; set; }

        #endregion
    }
}
